package com.example.mqttxprovider.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.mqttxprovider.callback.MqttConsumerCallBack;
import com.example.mqttxprovider.config.MqttConsumerConfig;
import com.example.mqttxprovider.entity.Message;
import com.example.mqttxprovider.service.MessageTreeService;

@RestController
@RequestMapping("/consumer")
public class MqttConsumerController {
    private static final Logger log = LoggerFactory.getLogger(MqttConsumerController.class);
    @Resource
    private MqttConsumerConfig client;
    @Resource
    private MessageTreeService messageTreeService;


    @Value("${spring.mqtt.client.id}")
    private String clientId;

    @PostMapping("/connect")
    public String connect() {
        client.connect();
        return clientId + "连接到服务器";
    }

    @PostMapping("/disConnect")
    public String disConnect() {
        client.disConnect();
        return client + "与服务器断开连接";
    }

    @PostMapping("/test")
    public String test() {
        try {
            Message message = new Message();
            message.setType("ss");
            message.setCreateTime(111L);
            message.setValue(123);
            messageTreeService.save(message);
            return "成功";
        } catch (Exception e) {
            log.error("error", e);
            return "失败";
        }
    }

    private static final String PRE = "raspberry/";

    @GetMapping("/getData")
    public Map<String, List<Message>> getData() {
        Map<String, List<Message>> m = new HashMap<>();
        List<String> names = Arrays.asList("raspberry/temperature",
                "raspberry/humidity", "raspberry/soil_moisture_percentage", "raspberry/soil_moisture_raw");
        List<Message> messagesList = messageTreeService.list();
        messagesList = messagesList.stream()
                .sorted(Comparator.comparing(Message::getCreateTime).reversed())
                .collect(Collectors.toList());
        for (Message message : messagesList) {
            String type = message.getType();
                List<Message> list = m.getOrDefault(type, new ArrayList<>());
                if (list.size() > 10){
                    continue;
                }
                list.add(message);
                m.put(type, list);

        }
        return m;
    }

    Message build(String type, Integer value) {
        Message message = new Message();
        message.setType(type);
        message.setValue(value);
        message.setCreateTime(System.currentTimeMillis() + (int)(Math.random() * 1000));
        return message;
    }
}
